import { getActivityList, getActivityDetail, createActivity, updateActivity, deleteActivity, getActivityTypes } from '@/api/activity'

const state = {
  activities: [],
  total: 0,
  loading: false,
  activityDetail: {},
  activityTypes: []
}

const mutations = {
  SET_ACTIVITIES: (state, activities) => {
    state.activities = activities
  },
  SET_TOTAL: (state, total) => {
    state.total = total
  },
  SET_LOADING: (state, loading) => {
    state.loading = loading
  },
  SET_ACTIVITY_DETAIL: (state, detail) => {
    state.activityDetail = detail
  },
  SET_ACTIVITY_TYPES: (state, types) => {
    state.activityTypes = types
  }
}

const actions = {
  // 获取活动列表
  getActivityList({ commit }, params) {
    commit('SET_LOADING', true)
    return new Promise((resolve, reject) => {
      getActivityList(params).then(response => {
        const { data } = response
        commit('SET_ACTIVITIES', data.records || [])
        commit('SET_TOTAL', data.total || 0)
        commit('SET_LOADING', false)
        resolve(data)
      }).catch(error => {
        commit('SET_LOADING', false)
        reject(error)
      })
    })
  },
  
  // 获取活动详情
  getActivityDetail({ commit }, id) {
    return new Promise((resolve, reject) => {
      getActivityDetail(id).then(response => {
        const { data } = response
        commit('SET_ACTIVITY_DETAIL', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 创建活动
  createActivity({ dispatch }, activityData) {
    return new Promise((resolve, reject) => {
      createActivity(activityData).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 更新活动
  updateActivity({ dispatch }, activityData) {
    return new Promise((resolve, reject) => {
      updateActivity(activityData).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 删除活动
  deleteActivity({ dispatch }, id) {
    return new Promise((resolve, reject) => {
      deleteActivity(id).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  
  // 获取活动类型
  getActivityTypes({ commit }) {
    return new Promise((resolve, reject) => {
      getActivityTypes().then(response => {
        const { data } = response
        commit('SET_ACTIVITY_TYPES', data)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} 